package org.wqp.redisson.main;

import com.alibaba.fastjson.JSON;
import org.redisson.api.RBlockingQueue;
import org.wqp.redisson.entity.User;
import org.wqp.redisson.utils.RedissonUtil;

import java.util.concurrent.TimeUnit;

public class RBlockingQueueTest {
    public static void main(String[] args) {
        RBlockingQueue<User> queue = RedissonUtil.getRedissonClient().getBlockingQueue("UserBlockingQueue");
        User user = new User(2, "blockingUser");
        //加入队列
        queue.offer(user);
        try {
            System.out.println("sleep..");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //从队头取出但是不删除
        User ret = queue.peek();
        System.out.println("peek = " + ret);

        //从队头取出并且删除
        ret = queue.poll();
        System.out.println("poll = " + ret);

        try {
            //从队头取出并且删除  当队列是空时，等待至超时
            ret = queue.poll(10, TimeUnit.MINUTES);
            System.out.println("ret = " + ret);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        RedissonUtil.getRedissonClient().shutdown();
    }
}
